package com.wqtz.chechai.adapter;

import android.graphics.Color;
import android.view.View;

import com.wqtz.chechai.R;
import com.wqtz.chechai.base.lvbaseadapter.LvBaseAdapter;
import com.wqtz.chechai.base.lvbaseadapter.ViewHolder;
import com.wqtz.chechai.bean.Order;
import com.yinglan.swiperefresh.SwipeMenu;
import com.yinglan.swiperefresh.SwipeMenuListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称： CheChai
 * 创建人 :  Kisum
 * 创建时间: 2017/1/13
 */

public class QuoteSecondFragmentAdapter extends LvBaseAdapter<Order> implements SwipeMenuListView.OnMenuItemClickListener {
    private List<Order> mDatas = new ArrayList<>();
    /**
     * 四种状态：审核中 审核成功 被拒绝 取消
     */
    private final int CHECKING = 0;
    private final int CHECK_SUCCESSED = 1;
    private final int REFUSED = 2;
    private final int CANCELD = 3;
    /**
     * 退款的三种状态： 退款审核中 已退款 退款被拒绝
     */
    private final int REFUND_CHECKING = 4;
    private final int REFUND_CHECK_SUCCESSED = 5;
    private final int REFUND_REFUSED = 6;
    /**
     * 解约的三种状态： 解约审核中 已解约 解约被拒绝
     */
    private final int BREAK_CHECKING = 7;
    private final int BREAK_CHECK_SUCCESSED = 8;
    private final int BREAK_REFUSED = 9;
    /**
     * 归档的三种状态： 归档审核中 已归档 归档被拒绝
     */
    private final int FILE_CHECKING = 10;
    private final int FILE_CHECK_SUCCESSED = 11;
    private final int FILE_REFUSED = 12;
    /**
     * 续约的三种状态： 续约审核中 已续约 续约被拒绝
     */
    private final int RENEW_CHECKING = 13;
    private final int RENEW_CHECK_SUCCESSED = 14;
    private final int RENEW_REFUSED = 15;

    QuoteItemClickCallBack mBack;

    public QuoteSecondFragmentAdapter(int resId) {
        super(resId);
    }

    public void setClickListen(QuoteItemClickCallBack mBack) {
        this.mBack = mBack;
    }

    @Override
    public void bindDatas(final List<Order> list, final int position, ViewHolder holder) {
        mDatas.clear();
        mDatas.addAll(list);
        Order order = list.get(position);
        holder.setText(R.id.carNumber, order.getCarNumber());
        holder.setText(R.id.tv_mortgage_user, order.getLoanName());
        holder.setText(R.id.tv_mortgage_time, order.getLoanDays());
        holder.setText(R.id.tv_car_info, order.getCarInfo());
        holder.setText(R.id.tv_check_info, order.getLoanInfo());
        holder.setText(R.id.tv_time, order.getLoanTime());
        holder.setText(R.id.tv_loan, order.getPrice());
        switch (order.getType()) {
            /**
             * 四种状态：审核中 审核成功 被拒绝 取消
             */
            case CHECKING:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_checking);
                //设置红色圆点可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.VISIBLE);
                //设置红色文本可见
                holder.getView(R.id.tv_loan).setVisibility(View.VISIBLE);
                //设置灰色圆点不可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.INVISIBLE);
                //设置灰色文本不可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.INVISIBLE);
                break;
            case CHECK_SUCCESSED:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_passed);
                //设置红色圆点可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.VISIBLE);
                //设置红色文本可见
                holder.getView(R.id.tv_loan).setVisibility(View.VISIBLE);
                //设置灰色圆点不可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.INVISIBLE);
                //设置灰色文本不可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.INVISIBLE);
                break;
            case REFUSED:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_rejected);
                //设置灰色圆点可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.VISIBLE);
                //设置灰色文本可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.VISIBLE);
                //设置红色圆点不可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.INVISIBLE);
                //设置红色文本不可见
                holder.getView(R.id.tv_loan).setVisibility(View.INVISIBLE);
                break;
            case CANCELD:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_canceled);
                //设置灰色圆点可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.VISIBLE);
                //设置灰色文本可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.VISIBLE);
                //设置红色圆点不可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.INVISIBLE);
                //设置红色文本不可见
                holder.getView(R.id.tv_loan).setVisibility(View.INVISIBLE);
                break;
            /**
             * 退款的三种状态： 退款审核中 已退款 退款被拒绝
             */
            case REFUND_CHECKING:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_refund_checking);
                //设置灰色圆点可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.VISIBLE);
                //设置灰色文本可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.VISIBLE);
                //设置红色圆点不可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.INVISIBLE);
                //设置红色文本不可见
                holder.getView(R.id.tv_loan).setVisibility(View.INVISIBLE);
                break;
            case REFUND_CHECK_SUCCESSED:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_refund_checked);
                //设置灰色圆点可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.VISIBLE);
                //设置灰色文本可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.VISIBLE);
                //设置红色圆点不可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.INVISIBLE);
                //设置红色文本不可见
                holder.getView(R.id.tv_loan).setVisibility(View.INVISIBLE);
                break;
            case REFUND_REFUSED:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_refund_rejected);
                //设置灰色圆点可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.VISIBLE);
                //设置灰色文本可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.VISIBLE);
                //设置红色圆点不可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.INVISIBLE);
                //设置红色文本不可见
                holder.getView(R.id.tv_loan).setVisibility(View.INVISIBLE);
                break;
            /**
             * 解约的三种状态： 解约审核中 已解约 解约被拒绝
             */
            case BREAK_CHECKING:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_break_checking);
                //设置灰色圆点可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.VISIBLE);
                //设置灰色文本可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.VISIBLE);
                //设置红色圆点不可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.INVISIBLE);
                //设置红色文本不可见
                holder.getView(R.id.tv_loan).setVisibility(View.INVISIBLE);
                break;
            case BREAK_CHECK_SUCCESSED:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_breaked);
                //设置灰色圆点可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.VISIBLE);
                //设置灰色文本可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.VISIBLE);
                //设置红色圆点不可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.INVISIBLE);
                //设置红色文本不可见
                holder.getView(R.id.tv_loan).setVisibility(View.INVISIBLE);
                break;
            case BREAK_REFUSED:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_break_rejected);
                //设置灰色圆点可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.VISIBLE);
                //设置灰色文本可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.VISIBLE);
                //设置红色圆点不可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.INVISIBLE);
                //设置红色文本不可见
                holder.getView(R.id.tv_loan).setVisibility(View.INVISIBLE);
                break;
            /**
             * 归档的三种状态： 归档审核中 已归档 归档被拒绝
             */
            case FILE_CHECKING:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_file_checking);
                //设置红色圆点可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.VISIBLE);
                //设置红色文本可见
                holder.getView(R.id.tv_loan).setVisibility(View.VISIBLE);
                //设置灰色圆点不可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.INVISIBLE);
                //设置灰色文本不可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.INVISIBLE);
                break;
            case FILE_CHECK_SUCCESSED:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_filed);
                //设置红色圆点可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.VISIBLE);
                //设置红色文本可见
                holder.getView(R.id.tv_loan).setVisibility(View.VISIBLE);
                //设置灰色圆点不可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.INVISIBLE);
                //设置灰色文本不可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.INVISIBLE);
                break;
            case FILE_REFUSED:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_file_rejected);
                //设置红色圆点可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.VISIBLE);
                //设置红色文本可见
                holder.getView(R.id.tv_loan).setVisibility(View.VISIBLE);
                //设置灰色圆点不可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.INVISIBLE);
                //设置灰色文本不可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.INVISIBLE);
                break;
            /**
             * 续约的三种状态： 续约审核中 已续约 续约被拒绝
             */
            case RENEW_CHECKING:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_renewal_checking);
                //设置红色圆点可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.VISIBLE);
                //设置红色文本可见
                holder.getView(R.id.tv_loan).setVisibility(View.VISIBLE);
                //设置灰色圆点不可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.INVISIBLE);
                //设置灰色文本不可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.INVISIBLE);
                break;
            case RENEW_CHECK_SUCCESSED:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_renewaled);
                //设置红色圆点可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.VISIBLE);
                //设置红色文本可见
                holder.getView(R.id.tv_loan).setVisibility(View.VISIBLE);
                //设置灰色圆点不可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.INVISIBLE);
                //设置灰色文本不可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.INVISIBLE);
                break;
            case RENEW_REFUSED:
                holder.setCirCleImag(R.id.iv_loan_status, R.drawable.loan_renewal_rejected);
                //设置红色圆点可见
                holder.getView(R.id.iv_cicle_red).setVisibility(View.VISIBLE);
                //设置红色文本可见
                holder.getView(R.id.tv_loan).setVisibility(View.VISIBLE);
                //设置灰色圆点不可见
                holder.getView(R.id.iv_cicle_grey).setVisibility(View.INVISIBLE);
                //设置灰色文本不可见
                holder.getView(R.id.tv_loan_grey).setVisibility(View.INVISIBLE);
                break;


        }
        holder.getViewHolder().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBack.oItemClickItem(position);
            }
        });
    }


    @Override
    public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
        switch (mDatas.get(position).getType()) {
            case CHECKING:
            case CHECK_SUCCESSED:
            case REFUND_CHECKING:
            case REFUND_CHECK_SUCCESSED:
            case BREAK_CHECKING:
            case BREAK_CHECK_SUCCESSED:
            case FILE_CHECKING:
            case FILE_CHECK_SUCCESSED:
            case RENEW_CHECKING:
            case RENEW_CHECK_SUCCESSED:
                menu.setViewType(1);
                break;
            case REFUSED:
            case CANCELD:
            case REFUND_REFUSED:
            case BREAK_REFUSED:
            case FILE_REFUSED:
            case RENEW_REFUSED:
                menu.setViewType(0);
                break;
        }
        mBack.oItemClickSwipeMenu(position, menu.getViewType());
        return false;

    }


    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas.size() == 0) {
            return 0;
        }
        switch (mDatas.get(position).getType()) {
            case CHECKING:
            case CHECK_SUCCESSED:
            case REFUND_CHECKING:
            case REFUND_CHECK_SUCCESSED:
            case BREAK_CHECKING:
            case BREAK_CHECK_SUCCESSED:
            case FILE_CHECKING:
            case FILE_CHECK_SUCCESSED:
            case RENEW_CHECKING:
            case RENEW_CHECK_SUCCESSED:
                return 1;
            case REFUSED:
            case CANCELD:
            case REFUND_REFUSED:
            case BREAK_REFUSED:
            case FILE_REFUSED:
            case RENEW_REFUSED:
                return 0;
        }
        return 1;
    }


    public interface QuoteItemClickCallBack {
        void oItemClickItem(int positon);

        void oItemClickSwipeMenu(int positon, int Type);
    }


}
